<template>
    <div>
        <el-table max-height="510px" :data="goodsList" border style="width: 100%">
            <el-table-column prop="id" label="商品编号" width="130">
            </el-table-column>
            <el-table-column prop="goodsname" label="商品名称" width="130">
            </el-table-column>
            <el-table-column prop="price" label="商品价格">
            </el-table-column>
            <el-table-column prop="market_price" label="市场价格">
            </el-table-column>
            <el-table-column prop="img" label="图片">
                <template slot-scope="item">
                    <div>
                        <img class="img" v-if="item.row.img" :src="$img + item.row.img" alt="">
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="是否新品" width="50">
                <template slot-scope="item">
                    <div>
                        {{ item.row.isnew == 1 ? "是" : "否" }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="是否热卖" width="50">
                <template slot-scope="item">
                    <div>
                        {{ item.row.ishot == 1 ? "是" : "否" }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="状态" width="50">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? "正常" : "禁用" }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button size="mini" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @current-change="changePage" background layout="prev, pager, next" :page-size="goodsSize"
            :total="goodsCount">
        </el-pagination>
    </div>
</template>

<script>
// 引入接口
import { goodsDel } from '../../request/api'
// 辅助性函数
import { mapGetters, mapActions } from 'vuex'
export default {
    computed: {
        ...mapGetters({
            goodsList: 'goods/getGoodsList', // 列表
            goodsCount: 'goods/getGoodsCount', // 总数
            goodsSize: 'goods/getSize'
        })
    },
    mounted() {
        // 页面加载就调用列表接口
        this.goodsListAction(),
            // 总数
            this.goodsCountAction()
    },
    methods: {
        // 当前页
        changePage(n) {
            console.log(n, '当前页');
            this.goodsPageAction(n)
        },
        ...mapActions({
            // 列表
            goodsListAction: 'goods/getGoodsListAction',
            // 总数
            goodsCountAction: 'goods/getGoodsCountAction',
            // 页码
            goodsPageAction: 'goods/getGoodsPageAction'
        }),
        //编辑事件
        edit(id) {
            this.$emit('edit', id)
        },
        // 删除事件
        del(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                goodsDel({ id })
                    .then(res => {
                        console.log(res, '商品删除');
                        if (res.code == 200) {
                            this.$message.success(res.msg)
                            // 重新调用列表接口
                            this.goodsListAction()
                            // 总数
                            this.goodsCountAction()
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
};
</script>

<style  lang="less" scoped>
.img{
    width: 150px;
    height: 150px;
}
</style>
